<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/11
 * Time: 16:46
 */

namespace app\sports_admin\model;


use think\Model;
use think\Request;
use traits\model\SoftDelete;

class News extends Model
{
    use SoftDelete;
    protected $table = "news";
    protected $ok    = "id";
    protected $autoWriteTimestamp = true;
    //体育资讯 列表
    public static function newsList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $adminList = self::where(function($query)use($searchName){if($searchName){$query->where('title','like',"%{$searchName}%");}})
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")
            ->select();

        $adminCount = self::where(function($query)use($searchName){if($searchName){$query->where('title','like',"%{$searchName}%");}})
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
    //体育资讯 详情
    public static function detail()
    {
        $id = Request::instance()->only('id');
        return self::where('id',$id['id'])->find();
    }
    //体育资讯 更新体育资讯
    public function updateHeart()
    {
        $data = Request::instance()->only('id,content,title,image');
        $content = htmlspecialchars($data['content']);
        return $this->save(['content'=>$content,'title'=>$data['title'],'image'=>$data['image']],['id'=>$data['id']]);
       // return self::where('id',$data['id'])->update(['content'=>$content,'title'=>$data['title'],'update_time'=>$time]);
    }
    //体育资讯 添加
    public static function add()
    {
        $data = Request::instance()->only('title,content,image');
        $content = htmlspecialchars($data['content']);
        return self::create(['title'=>$data['title'],'content'=>$content,'image'=>$data['image']]);
    }
    //删除体育资讯
    public static function delHeart()
    {
        $data = Request::instance()->only('id');
        return self::destroy($data['id']);
    }
}